package com.qin.db;

import oracle.jdbc.proxy.annotation.Pre;

import java.sql.*;

//(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.0.104)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=xepdb1)))

public class Connector {
	private String diverClass = "oracle.jdbc.driver.OracleDriver";
	private String url;
	private String userName;
	private String password;

	public Connector(String url, String userName, String password) {
		this.url = url;
		this.userName = userName;
		this.password = password;
	}

	public Connection getConnection() {
		Connection conn = null;
		try {
			//加载
			Class.forName(diverClass);
			//指定用户名和密码
			conn = DriverManager.getConnection(url, userName, password);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			System.out.println("加载数据库驱动失败");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return conn;
	}
	
	public boolean closeConnection(Connection conn, Statement stat) {
		
		try {
			//关闭连接
			if(stat != null){
				stat.close();
			}
			if(conn != null){
				conn.close();
			}
			return true;
			
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}

	}

	public boolean closeConnection(Connection conn, PreparedStatement pre) {

		try {
			//关闭连接
			if(pre != null){
				pre.close();
			}
			if(conn != null){
				conn.close();
			}
			return true;

		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}

	}

	public boolean closeConnection(Connection conn, Statement stat, ResultSet rs) {
		
		try {
			//关闭连接
			if(rs != null){
				rs.close();
			}
			if(stat != null){
				stat.close();
			}
			if(conn != null){
				conn.close();
			}
			return true;
			
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}

	}

	public boolean closeConnection(Connection conn, PreparedStatement pre, ResultSet rs) {

		try {
			//关闭连接
			if(rs != null){
				rs.close();
			}
			if(pre != null){
				pre.close();
			}
			if(conn != null){
				conn.close();
			}
			return true;

		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}

	}
}
